package com.mes.basicdata.manager;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mes.basicdata.dao.iface.IOperationDao;
import com.mes.basicdata.dao.iface.IRoutingOperationDao;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.domain.RoutingOperation;
import com.mes.basicdata.manager.iface.IOperationManager;
import com.mes.common.domain.BasicModel;
import com.mes.common.manager.BasicManager;
import com.mes.common.util.IdentityGenerator;

@Component
public class OperationManager extends BasicManager<Operation> implements IOperationManager {
	private final static Log log = LogFactory.getLog(OperationManager.class);
	
	@Autowired
	private IOperationDao operationDao;
	
	@Autowired
	private IRoutingOperationDao routingOperationDao;
	
	public void saveOperations(Collection<Operation>operations,String routingUid) throws Exception{
		if (null !=operations &&operations.size()>0) {
			for(Operation operation : operations){
				if(operation.getObjectState() == BasicModel.OBJECTSTATE_TOBEADDED){
					RoutingOperation routingOperation = new RoutingOperation();
					routingOperation.setUuid(IdentityGenerator.GetIdentityUUID());
					routingOperation.setObjectState(routingOperation.OBJECTSTATE_TOBEADDED);
					routingOperation.setOperationId(operation.getOperationId());
					routingOperation.setRoutingUid(routingUid);
					routingOperation.setOperationUid(operation.getUuid());
					routingOperationDao.save(routingOperation);
				}else if(operation.getObjectState() == BasicModel.OBJECTSTATE_TOBEDELETED){
					List<RoutingOperation> findByProperty = routingOperationDao.findByProperty("operationUid", operation.getUuid());
					routingOperationDao.delete(findByProperty);
				}else{
					List<RoutingOperation> routingOperations = routingOperationDao.findByProperty("operationUid", operation.getUuid());
					for (RoutingOperation routingOperation : routingOperations) {
						routingOperation.setOperationId(operation.getOperationId());
						routingOperation.setRoutingUid(routingUid);
						routingOperation.setOperationUid(operation.getUuid());
					}
					routingOperationDao.update(routingOperations);
				}
				operationDao.save(operation);
			}
		}
	}
	
	
//	public Operation sync(Operation object) throws Exception {
//		if (object == null) {
//			return null;
//		}
//		String objectClassName = object.getClass().getName();
//		
//		try {
//			if (object.getOutUid() == null
//					|| (object.getOutUid() != null && "".equals(object.getOutUid()))) {
//				log.error("给出的对象 " + objectClassName + " 没有给出outUid");
//				throw new Exception("给出的对象 " + objectClassName + " 没有给出outUid");
//			}
//			List<Operation> dbObjects = operationDao.getByOutUid(object.getOutUid());
//			if(dbObjects == null){
//				log.info("根据外键：" + object.getOutUid() + "没有找到 " + objectClassName + " 相同记录，则添加新对象");
//				object.setNotes("integrated");
//				object.setUuid(SysUtil.getUUID());
//				this.add(object);
//				log.info("sync object " + objectClassName + " successfully");
//				return object;
//			} else {
//				if(dbObjects.size() == 0){
//					log.info("根据外键：" + object.getOutUid() + "没有找到 " + objectClassName + " 相同记录，则添加新对象");
//					object.setNotes("integrated");
//					object.setUuid(SysUtil.getUUID());
//					this.add(object);
//					log.info("sync object " + objectClassName + " successfully");
//					return object;
//				} else if(dbObjects.size() == 1) {
//					if (object.getIsDelete() != null && object.getIsDelete()) {
//						Operation dbObject = dbObjects.get(0);
//						this.delete(dbObject);
//						return object;
//					}else {
//						Operation dbObject = dbObjects.get(0);
//						object.setUuid(dbObject.getUuid());
//						ObjectUtil.copyAttributes(object, dbObject);
//						dbObject.setChangeTime(new Date());
//						this.update(dbObject);
//						log.info("sync object " + objectClassName + " successfully");
//						return dbObject;
//					}
//				} else {// 如果有多个相同的记录，则报错
//					log.error("给出的对象 " + objectClassName + " 给出的outUid：" + object.getOutUid() + "在数据库中对应了多条记录！");
//					throw new Exception("给出的对象 " + objectClassName + " 给出的outUid：" + object.getOutUid() + "在数据库中对应了多条记录！");
//				}
//			}
//
//		} catch (Exception e) {
//			log.error("sync object " + objectClassName + " failed!");
//			e.printStackTrace();
//			throw e;
//		}
//	}
	
	
}
